<?php

/**
 * 微信用户项目服务
 */

namespace App\Services\WeChat;

use App\Repositories\WeChat\WeChatUserRepository;
use App\Repositories\WeChat\WeChatUserXCXRepository;

class WeChatUserService
{
    private $weChatUserRepository;
    private $weChatUserXCXRepository;
    private $config = [];

    public function __construct(
        WeChatUserRepository $weChatUserRepository,
        WeChatUserXCXRepository $weChatUserXCXRepository
    )
    {
        $this->weChatUserRepository = $weChatUserRepository;
        $this->weChatUserXCXRepository = $weChatUserXCXRepository;
    }

    public function setConfig($config)
    {
        $this->config = $config;
        $this->weChatUserRepository->_config($config);
        $this->weChatUserXCXRepository->_config($config);
    }

    private function factory()
    {
        switch ($this->config['bindRef']) {
            case 10:
                $objRepository = $this->weChatUserRepository;
                break;
            case 20:
                $objRepository = $this->weChatUserXCXRepository;
                break;
            default :
                $objRepository = $this->weChatUserRepository;
                break;
        }

        return $objRepository;
    }

    public function authorizeUrl($scopes, $callback)
    {
        $getTargetUrl = $this->weChatUserRepository->authorizeUrl($scopes, $callback);

        return $getTargetUrl;
    }


    public function authorizeToken($code, $extend)
    {
        $obj = $this->factory();

        return $obj->authorizeToken($code, $extend);
//        return $this->weChatUserRepository->authorizeToken($code, $extend);
    }


    public function authorizeUser($openID, $accessToken)
    {
        return $this->weChatUserRepository->authorizeUser($openID, $accessToken);
    }
}